﻿using ATEDiagnoser;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AteCapybara
{
    public partial class QRCodeShow : Form
    {
        DataTable dt;
        public QRCodeShow(DataTable dt)
        {
            InitializeComponent();
            this.dt = dt;
        }

        private void QRCodeShow_Load(object sender, EventArgs e)
        {
            //为dropdownlist赋值
            dropdownlist1.Items.Clear();
            foreach (DataRow item in dt.Rows)
            {
                dropdownlist1.Items.Add(item[0].ToString().Trim());
            }
            dropdownlist1.SelectedIndex = 0;

            label1.Text = "账号";
            //30分钟后消失
            timer1.Interval = 1800000;
            timer1.Enabled = true;
            timer1.Start();
        }

        DataRow selectedRow;
        int selectedColumnIndex;

        private void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (DataRow item in dt.Rows)
            {
                if (item[0].ToString().Trim()== dropdownlist1.Text)
                {
                    selectedRow = item;
                    selectedColumnIndex = 1;
                    this.Text = dropdownlist1.Text;
                    label1.Text = "账号";
                    Change();
                }
            }
            
        }

        private void Change()
        {
            textBox1.Text = selectedRow[selectedColumnIndex].ToString().Trim();
            picbox1.Image = GetQRCode.Proudce(textBox1.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            selectedColumnIndex++;
            label1.Text = "密码";
            if (selectedColumnIndex > 2) 
            {
                selectedColumnIndex = 1;
                label1.Text = "账号";
            }
            Change();
        }

        private void textBox1_MouseDown(object sender, MouseEventArgs e)
        {
            TextBoxBase richTextBox = sender as TextBox;

            if (!string.IsNullOrEmpty(richTextBox.Text))
            {
                // 选择全部文本
                richTextBox.SelectAll();
                try
                {
                    Clipboard.SetText(richTextBox.Text.Trim());
                }
                catch (Exception ee)
                {
                    MsgShow.Error("复制出错！" + ee.Message);
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            timer1.Enabled = false;
            this.Close();
        }
    }
}
